﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class SanPham : Form
    {
        string tenanh;
        string idsp;
        public SanPham()
        {
            InitializeComponent();
        }

        private void SanPham_Load(object sender, EventArgs e)
        {
            load_DSSP();
        }

        private void load_DSSP()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from SANPHAM join LOAISANPHAM on SANPHAM.IDloai = LOAISANPHAM.IDloai";
            SqlCommand cmd = new SqlCommand(sql,conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table SanPham");
            DataTable dt = ds.Tables["Table SanPham"];

            DSSP.AutoGenerateColumns = false;
            DSSP.DataSource = ds.Tables["Table SanPham"].DefaultView;
        }

        private void load_loaisp()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from LOAISANPHAM";
            SqlCommand cmd = new SqlCommand(sql, conn);

            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Table LOAISANPHAM");
            DataTable dt = ds.Tables["Table LOAISANPHAM"];

            loaisp.DisplayMember = "tenloai";
            loaisp.ValueMember = "IDloai";
            loaisp.DataSource = ds.Tables["Table LOAISANPHAM"].DefaultView;
        }

        private void Auto_load()
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "select * from SANPHAM join LOAISANPHAM on SANPHAM.IDloai = LOAISANPHAM.IDloai where IDsanpham = @id";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@id", idsp);

            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            while (r.Read())
            {
                tsp.Text = r["tensanpham"].ToString();
                loaisp.SelectedValue = r["IDloai"].ToString();
                ngaysp.Value = (DateTime)r["ngay"];
                gia.Text = r["giaban"].ToString();
                hbh.Text = r["hanbaohanh"].ToString();
                dvt.Text = r["donvitinh"].ToString();
                tenanh = r["hinhanh"].ToString();
                if (tenanh != "")
                    pictureBox1.Image = Image.FromFile(@"C:\Users\FOX\Desktop\1stwed\1stVATCWed\Img\" + tenanh);
                else
                    pictureBox1.Image = null;
            }
            r.Close();
            conn.Close();
        }

        private void load_null()
        {
            tsp.Text = "";
            loaisp.SelectedValue = "";
            pictureBox1.Image = null;
            gia.Text = "";
            hbh.Text = "";
            dvt.Text = "";
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            idsp = DSSP.Rows[e.RowIndex].Cells["IDsanpham"].Value.ToString();
            load_loaisp();
            Auto_load();
        }

        private void loadanhsp_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            if(f.ShowDialog() == DialogResult.OK)
            {
                string filename = f.FileName;
                tenanh = System.IO.Path.GetFileName(f.FileName);
                pictureBox1.Image = Image.FromFile(filename);
            }
        }

        private void Add_Click(object sender, EventArgs e)
        {
            SP_ADD f = new SP_ADD();
            if (f.ShowDialog() == DialogResult.OK)
            {
                load_DSSP();
            }
        }

        private void Upd_Click(object sender, EventArgs e)
        {
            Config file = new Config(Application.StartupPath + "\\config.ini");
            string connectionString = file.ReadValue("server", "conn");
            SqlConnection conn = new SqlConnection(connectionString);
            string sql = "update SANPHAM set tensanpham = @tsp,IDloai = @loai,hinhanh = @hinhanh,ngay = @ngay,giaban = @giaban,hanbaohanh = @hbh,donvitinh = @dvt where IDsanpham = @idsp";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@idsp", idsp);
            cmd.Parameters.AddWithValue("@tsp", tsp.Text);
            cmd.Parameters.AddWithValue("@loai", loaisp.SelectedValue);
            if(pictureBox1.Image == null)
                cmd.Parameters.AddWithValue("@hinhanh","");
            else            
                cmd.Parameters.AddWithValue("@hinhanh", tenanh);
            cmd.Parameters.AddWithValue("@ngay", ngaysp.Value.ToShortDateString());
            cmd.Parameters.AddWithValue("@giaban", gia.Text);
            cmd.Parameters.AddWithValue("@hbh", hbh.Text);
            cmd.Parameters.AddWithValue("@dvt", dvt.Text);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            load_DSSP();
        }

        private void Del_Click(object sender, EventArgs e)
        {
            if ((MessageBox.Show("Bạn có muốn xóa dữ liệu không ?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
            {
                int i = DSSP.SelectedRows[0].Index;
                string id = DSSP.Rows[i].Cells["IDsanpham"].Value.ToString();
                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "conn");                
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "delete from SANPHAM where IDsanpham = @id";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);

                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();

                DSSP.Rows.RemoveAt(i);
                load_null();
            }
        }
    }
}
